package com.woniuxy.usecredit.controller;/* *
@author author
@create 2021-11-15 20:56
*/

import com.woniuxy.entity.BusinessInfo;
import com.woniuxy.entity.EnterpriseCredit;
import com.woniuxy.entity.EnterpriseCreditUsed;
import com.woniuxy.service.ActivitiService;
import com.woniuxy.service.business.BorrowDemandService;
import com.woniuxy.usecredit.service.EnterpriseCreditUsedService;
import com.woniuxy.utils.ResponseResult;
import com.woniuxy.utils.ResultStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/creditUsed")
public class EnterpriseCreditUsedController {

    @Autowired
    EnterpriseCreditUsedService enterpriseCreditUsedService;

    @Autowired
    ActivitiService activitiService;

    @Autowired
    BorrowDemandService borrowDemandService;




    @PostMapping("/")
    public ResponseResult addCreditUsed(EnterpriseCreditUsed enterpriseCreditUsed){
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(200);
        enterpriseCreditUsedService.addCreditUsed(enterpriseCreditUsed);
        responseResult.setMessage("添加用信记录成功");
        return responseResult;
    }

    @GetMapping("/{creditId}")
    public ResponseResult getCreditUsed(@PathVariable("creditId") int creditId){
        ResponseResult responseResult = new ResponseResult();
        List<EnterpriseCreditUsed> creditUseds = enterpriseCreditUsedService.selectCreditUsedByCreditId(creditId);
        responseResult.setCode(200);
        responseResult.setMessage("获取核心企业用信情况成功");
        responseResult.setStatus(ResultStatus.HAVE_PERMS);
        responseResult.setData(creditUseds);
        return responseResult;
    }

    @GetMapping("/businessName/{businessName}")
    public ResponseResult getCreditUsedByBusinessName(@PathVariable("businessName") String businessName){
        ResponseResult responseResult = new ResponseResult();
        ResponseResult businessInfoRR = borrowDemandService.getBusinessInfoByBusinessName(businessName);
        BusinessInfo businessInfo = (BusinessInfo) businessInfoRR.getData();
        List<EnterpriseCredit> credits = activitiService.findCreditByEnterpriseId(businessInfo.getId());
        for (EnterpriseCredit ec:credits) {
            ec.setCoreEnterprise(businessInfo);
        }
        List<EnterpriseCreditUsed> allCreditUsed = new ArrayList<EnterpriseCreditUsed>();
        for (EnterpriseCredit ec:credits) {
            List<EnterpriseCreditUsed> creditUseds = enterpriseCreditUsedService.selectCreditUsedByCreditId(ec.getId());
            for (EnterpriseCreditUsed ecu:creditUseds) {
                ecu.setEnterpriseCredit(ec);
            }
            allCreditUsed.addAll(creditUseds);
        }
        responseResult.setCode(200);
        responseResult.setMessage("获取核心企业用信情况成功");
        responseResult.setStatus(ResultStatus.HAVE_PERMS);
        responseResult.setData(allCreditUsed);
        return responseResult;
    }

    @GetMapping("/all")
    public ResponseResult getAllCreditUsed(){
        ResponseResult responseResult = new ResponseResult();
        List<EnterpriseCreditUsed> creditUseds = enterpriseCreditUsedService.selectAllCreditUseds();
        for (EnterpriseCreditUsed ecu:creditUseds) {
            EnterpriseCredit credit = activitiService.findCreditById(ecu.getEnterpriseCredit().getId());
            ResponseResult businessInfoRR = borrowDemandService.getBusinessInfoById(credit.getCoreEnterprise().getId());
            credit.setCoreEnterprise((BusinessInfo) businessInfoRR.getData());
            ecu.setEnterpriseCredit(credit);
        }
        responseResult.setCode(200);
        responseResult.setMessage("获取核心企业用信情况成功");
        responseResult.setStatus(ResultStatus.HAVE_PERMS);
        responseResult.setData(creditUseds);
        return responseResult;
    }

    @DeleteMapping("/{id}")
    public ResponseResult removeCreditUsed(@PathVariable("id") int id){
        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(200);
        boolean isRemove = enterpriseCreditUsedService.removeCreditUsedById(id);
        if(isRemove){
            responseResult.setMessage("删除用信记录成功");
        }else{
            responseResult.setMessage("删除用信记录失败");
        }
        return responseResult;
    }
}
